package com.rjwh.dingdong.client.activity.homework.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.HomeWorkFansFollowListAdapter;
import com.rjwh.dingdong.client.bean.localbean.HomeWorkFansFollowListItemData;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.HomeWorkFansFollowListPresenter;
import com.rjwh.dingdong.client.mvp.view.HomeWorkFansFollowListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 王小珂
 * @ClassName HomeWorkFansListFragment
 * @Description 粉丝/关注 列表
 * @date 2017-1-6
 * @history 2017-1-6 author: description:
 */

public class HomeWorkFansListFragment extends BaseFragment implements HomeWorkFansFollowListView, HomeWorkFansFollowListAdapter.OnFollowBtnClickListener {

    @BindView(R.id.home_work_fans_list)
    ListView homeWorkFansList;
    @BindView(R.id.home_work_fans_mPullRefreshView)
    SwipyRefreshLayout homeWorkFansMPullRefreshView;
    private int type;
    private boolean isInit;
    private int pageNum;
    private HomeWorkFansFollowListPresenter presenter;
    private HomeWorkFansFollowListAdapter adapter;

    private HomeWorkFansFollowListItemData followData;
    private int currentPosition;

    public static HomeWorkFansListFragment newInstance(int type) {
        HomeWorkFansListFragment fragment = new HomeWorkFansListFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type", 1);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.home_work_fans_layout_fragment_layout, container, false);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {
        homeWorkFansMPullRefreshView.setDirection(SwipyRefreshLayoutDirection.TOP);
        homeWorkFansMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNum = 0;
                        doGetHomeWorkFansFollowList(true, SwipyRefreshLayoutDirection.TOP);
                        break;
                    case BOTTOM:
                        pageNum++;
                        doGetHomeWorkFansFollowList(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
        adapter = new HomeWorkFansFollowListAdapter(getContext(), this);
        homeWorkFansList.setAdapter(adapter);
    }

    private void doGetHomeWorkFansFollowList(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetHomeWorkFansFollowList(type, pageNum, isRefresh, direction);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new HomeWorkFansFollowListPresenter(getContext(), this);
        if (getUserVisibleHint() && isVisible() && isInit) {
            doRefresh();//加载数据的方法
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断是否可见 并且是否已经初始化过，以防空指针
        if (isVisibleToUser && isVisible() && isInit) {
            doRefresh();//加载数据的方法
        } else {
            isInit = false;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isInit = true;//初始化后 将其设置成true  表示已经初始化控件过了
    }


    public void doRefresh() {
        if (homeWorkFansMPullRefreshView != null) {
            homeWorkFansMPullRefreshView.doPullRefreshing();
        }
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        homeWorkFansMPullRefreshView.setRefreshing(isRefresh);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        homeWorkFansMPullRefreshView.setDirection(direction);
    }

    @Override
    public void loadData(boolean isRefresh, List<HomeWorkFansFollowListItemData> homeWorkFansFollowList) {
        if (isRefresh) {
            adapter.clearAll();
        }
        adapter.appendToList(homeWorkFansFollowList);
    }

    @Override
    public void loadFollowState() {
        String isfollow = followData.getIsfollow();
        if (type == 1) {
            switch (isfollow) {
                case "2":
                    //显示 "互相关注"  点击后执行取消关注,取消成功后显示"未关注"
                    followData.setIsfollow("3");
                    break;
                case "3":
                    //显示 "未关注"  点击后执行关注,成功后显示"互相关注"
                    followData.setIsfollow("2");
                    break;
            }
        } else {
            switch (isfollow) {
                case "1":
                    //显示 "关注"  点击后执行取消关注,取消成功后删除条目
                case "2":
                    //显示 "互相关注"  点击后执行取消关注,取消成功后删除条目
                    adapter.getList().remove(currentPosition);
                    break;
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showLoadingDialog() {
        showLoadDialog();
    }

    @Override
    public void dismissLoadingDialog() {
        dismissLoadDialog();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

    @Override
    public void onFollowBtnClick(final HomeWorkFansFollowListItemData data, final int position) {
        followData = data;
        currentPosition = position;

        String isfollow = data.getIsfollow();

        if (type == 1) {
            switch (isfollow) {
                case "2":
                    //显示 "互相关注"  点击后执行取消关注,取消成功后显示"未关注"
                    presenter.doHomeWorkFollowOperation("0", data.getUserid());
                    break;
                case "3":
                    //显示 "未关注"  点击后执行关注,成功后显示"互相关注"
                    presenter.doHomeWorkFollowOperation("1", data.getUserid());
                    break;
            }
        } else {
            switch (isfollow) {
                case "1":
                    //显示 "关注"  点击后执行取消关注,取消成功后删除条目
                case "2":
                    //显示 "互相关注"  点击后执行取消关注,取消成功后删除条目
                    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                    builder.setMessage("确定不再关注此人")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    presenter.doHomeWorkFollowOperation("0", data.getUserid());
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
            }
        }
    }
}
